using Asp.Versioning;
using Asp.Versioning.Conventions;
using Microsoft.Extensions.Options;
using Swagger.Shared;
using Swashbuckle.AspNetCore.SwaggerGen;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
// 添加 Swagger
builder.Services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();
builder.Services.AddSwaggerGen(options =>
{
    options.OperationFilter<SwaggerDefaultValues>();
});
builder.Services.AddApiVersioning(options =>
{
	options.AssumeDefaultVersionWhenUnspecified = true; //当未指定版本时，使用默认版本
	options.DefaultApiVersion = new ApiVersion(1, 0); //默认版本
	options.ReportApiVersions = true; //返回支持的版本信息
	options.ApiVersionReader = new UrlSegmentApiVersionReader();
	options.Policies.Sunset(1.0) //配置Sunset
		.Effective(2025, 6, 1)
		.Link("https://docs.api.com/policy.html?api-version=1.0")
		.Title("API Policy")
		.Type("text/html");

	options.Policies.Sunset(2.0)
		.Effective(2025, 12, 1)
		.Link("https://docs.api.com/policy.html?api-version=2.0")
		.Title("API Policy")
		.Type("text/html");
}).AddMvc(
	// 命名空间版本约定
	options => options.Conventions.Add(new VersionByNamespaceConvention()
	)).AddApiExplorer(options =>
{
	// 定义API文档分组的格式，这里设置为'v'加上版本号。
	options.GroupNameFormat = "'v'V";
	// 在API文档的URL中替换版本号
	options.SubstituteApiVersionInUrl = true;
});

var app = builder.Build();
app.UseSwaggerWithUi();
app.UseHttpsRedirection();
app.MapControllers();
app.Run();
